package com.jiujichina.star.dao;

import com.jiujichina.star.config.EsConfig;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

/*@Component*/
public class EsClient {

   // private static Logger logger = LoggerFactory.getLogger(EsClient.class);

    public static TransportClient client = null;

    private static String clusterName = EsConfig.getEsClusterName();//设置集群名称
    private static boolean sniff = EsConfig.isEsSniff();// 是否自动检测变化节点
    private static String pingTimeout = EsConfig.getEsPingTimeout();// 等待来自节点的ping响应的时间。默认为5 s
    private static boolean ignoreClusterName = EsConfig.isEsIgnoreClusterName();//是否忽略集群名称
    private static String nodesSamplerInterval = EsConfig.getEsNodesSamplerInterval();// 对列出和连接的节点进行采样的频率。默认为5 s
    private static String IP = EsConfig.getEsIP();
    private static int PORT = EsConfig.getEsPORT();  //端口

    static {
        try {
            //logger.info("ES初始化配置开始");
            Settings settings = Settings.builder().put("cluster.name", clusterName).put("client.transport.sniff", sniff)
                    .put("client.transport.ignore_cluster_name", ignoreClusterName)
                    .put("client.transport.ping_timeout", pingTimeout)
                    .put("client.transport.nodes_sampler_interval", nodesSamplerInterval).build();
            client = new PreBuiltTransportClient(settings);
            if (IP != null) {
                if (IP.indexOf(",") != -1) {
                    for (String ip : IP.split(",")) {
                        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), Integer.valueOf(PORT)));
                    }
                } else {
                    client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(IP), Integer.valueOf(PORT)));
                }
            }
            //  initES();
           // logger.info("ES 集群{}初始化配置====SUCCESS", clusterName);
        } catch (Exception e) {
           // logger.error("ES初始化配置失败:{}", e.getMessage(), e);
        }
    }

    public static SearchResponse queryDataPage(String index, String type, QueryBuilder queryBuilder, int offset, int rows)throws Exception{
        SortBuilder sortBuilder = SortBuilders.fieldSort("createTime").order(SortOrder.DESC);
        SearchResponse searchResponse = null;
        try {
            searchResponse = client.prepareSearch(index).setTypes(type).setQuery(queryBuilder).addSort(sortBuilder).setFrom(offset).setSize(rows).execute().actionGet();
        }catch(SearchPhaseExecutionException e){
            throw  e;
        }
        return searchResponse;
    }


}
